package DimensionGui;
import javax.swing.JPanel;
import javax.swing.JComboBox;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Scrollbar;
import java.awt.TextField;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.NumberFormat;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JFormattedTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;

import org.jhotdraw.figures.AttributeFigure;

import DimensionTools.DimensionTool;
import FloorComponents.DimensionableFigure;

public class MyPane extends JPanel {
	AttributeFigure _shape;
	DimensionableFigure _oldShape;
	DimensionTool _dt;
	JFrame _frame;
	TextField _w,_h;

	/**
	 * Create the panel. This is the panel to put in the window
	 */
	public MyPane(DimensionTool dt, JFrame container) {
		_dt = dt;
		_frame = container;
	// use oldshape in case of cancel button getting pressed
		// maybe needs to hide?
		
		// dangerous type cast , however everything should be an Attribute figure, watch out for door
		_oldShape =dt.getSlave();
		_shape =(AttributeFigure) dt.getSlave();
		
		
		

		setLayout(null);

		// Setting up textfields, Before scrollbars so they can be called
		// need intial values maybe text field is the way to go
		// -------------------
		Dimension d =_shape.displayBox().getSize();
		
		MyTextAreaHeight heightTextField = new MyTextAreaHeight (_shape);
		heightTextField.setText(Integer.toString((int) d.getHeight()));
		heightTextField.setBounds(49, 81, 84, 20);
		heightTextField.addActionListener(new TextAreaActionListener(
				heightTextField));
		add(heightTextField);
		_h = heightTextField;
		
		
		
		

		MyTextAreaWidth widthTextField = new MyTextAreaWidth (_shape);
		widthTextField.setText(Integer.toString((int) d.getWidth()));
		widthTextField.setBounds(49, 162, 84, 20);
		widthTextField.addActionListener(new TextAreaActionListener(widthTextField));
		
		
		add(widthTextField);
		
		_w= widthTextField;

		// ------------------------------------

		JLabel lblNewLabel = new JLabel("Height");
		lblNewLabel.setBounds(49, 42, 84, 14);
		add(lblNewLabel);

		JLabel label = new JLabel("Width");
		label.setBounds(49, 123, 46, 14);
		add(label);

		// Setting up scroll incrementer
		// ----------------------
		TextboxScrollBar height_scrollbar = new TextboxScrollBar(heightTextField);
		height_scrollbar.setBlockIncrement(1);
		height_scrollbar.setMaximum(100000);
		height_scrollbar.setMinimum(-100000);
		height_scrollbar.addAdjustmentListener(new MyIncrementerScroll(
				height_scrollbar));
		height_scrollbar.setBounds(146, 81, 17, 32);
		add(height_scrollbar);

		

		// set up thing in right order
		
		TextboxScrollBar width_scrollbar = new TextboxScrollBar(widthTextField);
		width_scrollbar.setBlockIncrement(1);
		width_scrollbar.setMaximum(100000);
		width_scrollbar.setMinimum(-100000);
		width_scrollbar.addAdjustmentListener(new MyIncrementerScroll(
				width_scrollbar));
		width_scrollbar.setBounds(146, 162, 17, 32);
		add(width_scrollbar);
		
		
		
		

		// ---------------------------------------------

		JCheckBox chckbxDisplayDimensions = new JCheckBox("Display Dimensions");
		chckbxDisplayDimensions.setBounds(49, 273, 202, 32);
		add(chckbxDisplayDimensions);
		
		
		
		
		
		
//------BUTTONS 
		JButton btnOk = new JButton("Ok");
		btnOk.setBounds(32, 367, 89, 23);
		btnOk.addMouseListener(new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent e) {
				int h = Integer.parseInt(_h.getText());
				int w = Integer.parseInt(_w.getText());
				
				
				// incase user doesn't press enter this should save the changes
				
				Rectangle r = _shape.displayBox();
				r.setSize(w, h);
				_shape.displayBox(r);

				
				
				
				_frame.dispose();
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		add(btnOk);
		
		
		
		
//Cancels close windows and stores no changes
		
		
		JButton button = new JButton("Cancel");
		button.setBounds(162, 367, 89, 23);
		button.addMouseListener(new MouseListener(){
			

			@Override
			public void mouseClicked(MouseEvent arg0) {
				_dt.setSlave(_oldShape);
				_frame.dispose();
				
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		
		add(button);

	}

}
